package com.example.comiccenter.service.implt;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.comiccenter.dao.CarouselMapper;
import com.example.comiccenter.domain.Carousel;
import com.example.comiccenter.service.CarouselService;
import com.example.comiccenter.service.CollectService;
import com.example.comiccenter.utils.ResultData;
import com.example.comiccenter.utils.SearchBean;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.io.File;
import java.io.IOException;

import static com.example.comiccenter.utils.Consts.rootPath;
import static com.example.comiccenter.utils.Consts.targetRootPath;

@Service
public class CarouselServiceImpl implements CarouselService {

    @Resource
    private CarouselMapper carouselMapper;

    @Override
    public ResultData getAllCarousel(SearchBean<Carousel> searchBean) {
        QueryWrapper<Carousel> wrapper = new QueryWrapper<>();
        wrapper.eq(searchBean.getData().getComicName()!=null,"comicName",searchBean.getData().getComicName());
        Page<Carousel> carouselPage = carouselMapper.selectPage(new Page<>(searchBean.getPageNum(), searchBean.getPageSize()), wrapper);
        return ResultData.success("查询成功",carouselPage);
    }

    //增加轮播图
    //如果图片不存在，返回报错。
    //1.增加记录
    //2.增加图片（新增文件夹）
    @Override
    public ResultData addCarousel(Carousel carousel, MultipartFile picture) {
        QueryWrapper<Carousel> wrapper = new QueryWrapper<>();
        wrapper.eq("comicName",carousel.getComicName());
        if (carouselMapper.selectCount(wrapper)>0){
            return ResultData.error(400,"该漫画已经添加到轮播图中");
        }
        if (picture==null || picture.isEmpty()){
            return ResultData.error(400,"请上传图片");
        }
        String foldePath = rootPath + "/carousel/";
        String targetFoldePath = targetRootPath + "/carousel/";
        File file = new File(foldePath);
        if (!file.exists()){
            file.mkdir();
        }
        String filePath = foldePath + carousel.getComicName() + ".jpg";
        String targetFilePath = targetFoldePath + carousel.getComicName() + ".jpg";
        try {
            FileUtils.copyInputStreamToFile(picture.getInputStream(),new File(filePath));
            FileUtils.copyInputStreamToFile(picture.getInputStream(),new File(targetFilePath));
            carousel.setPicture("/images/carousel/"+carousel.getComicName()+".jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
        carouselMapper.insert(carousel);

        return ResultData.success("添加成功");
    }

    @Override
    public ResultData deleteCarousel(Carousel carousel) {
        Carousel carousel1 = carouselMapper.selectById(carousel.getId());
        String path = rootPath + "/carousel/" + carousel1.getComicName() + ".jpg";
        String targetPath = targetRootPath + "/carousel/" + carousel1.getComicName() + ".jpg";

        File file = new File(path);
        File file1 = new File(targetPath);
        if (file.exists()){
            file.delete();
        }
        if (file1.exists()){
            file1.delete();
        }
        carouselMapper.deleteById(carousel);
        return ResultData.success("删除成功");
    }

    @Override
    public ResultData updateCarousel(Carousel carousel, MultipartFile picture) {
        return null;
    }
}
